package com.woniuxy.lab.woniu_lab_boss.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.lab.woniu_lab_boss.dao.AccountDao;
import com.woniuxy.lab.woniu_lab_boss.exception.AccountException;
import com.woniuxy.lab.woniu_lab_boss.exception.TeacherException;
import com.woniuxy.lab.woniu_lab_boss.exception.WebException;
import com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode;
import com.woniuxy.lab.woniu_lab_boss.model.*;
import com.woniuxy.lab.woniu_lab_boss.dao.AdminDao;
import com.woniuxy.lab.woniu_lab_boss.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lab.woniu_lab_boss.service.dto.PlatformDto;
import com.woniuxy.lab.woniu_lab_boss.util.JwtUtil;
import com.woniuxy.lab.woniu_lab_boss.util.WebUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode.*;
import static com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode.DATA_ERROR;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements AdminService {
    public static final String TEACHER = "TEACHER";
    public static final String LAP_BOSS_TOKEN = "lap-boss-token";
    @Resource
    private UserService userService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private RangeService rangeService;
    @Resource
    private AccountService accountService;

    @Override
    public PlatformDto getPlatformInfo() {
        PlatformDto platformDto = new PlatformDto();
        List<User> users = userService.list(new QueryWrapper<User>().eq("status", "ENABLE"));
        platformDto.setTotalUser(users.size());
        List<Teacher> teachers = teacherService.list(new QueryWrapper<Teacher>().eq("status", "ENABLE"));
        platformDto.setTotalTeacher(teachers.size());
        List<Range> ranges = rangeService.list();
        platformDto.setTotalRange(ranges.size());
        return platformDto;
    }

    @Override
    public Integer getAdminId() {
        //判断token是否为空
        String token = WebUtil.getRequest().getHeader(LAP_BOSS_TOKEN);
        if (ObjectUtil.isEmpty(token)) {
            throw new AccountException(NOT_LOGIN);
        }
        //从token中获取ID
        Integer accountId = JwtUtil.getUserIdFromReq();
        String userType = JwtUtil.getUserTypeFromReq();
        Integer adminId = JwtUtil.getUserPersonFromReq();
        //非讲师账号
        if (!userType.equals("ADMIN")) {
            throw new TeacherException(ADMIN_STATUS_EXCEPTION);
        }
        Account account = accountService.getById(accountId);
        //账号不存在
        if (ObjectUtil.isEmpty(account)) {
            throw new AccountException(ACCOUNT_IS_NOT_EXISTS);
        }
        //数据有误
        if (!account.getPersonId().equals(adminId)) {
            throw new TeacherException(DATA_ERROR);
        }
        return adminId;
    }

    @Override
    public void changePassword(Integer adminId, String password, String newPassword) {
        Admin admin = getById(adminId);
        if (admin == null) throw new WebException(WebExceptionCode.ADMIN_IS_NOT_EXISTS);
        if (!new BCryptPasswordEncoder().matches(password, admin.getPassword()))
            throw new WebException(WebExceptionCode.PASSWORD_IS_WRONG);
        admin.setPassword(new BCryptPasswordEncoder().encode(newPassword));
        updateById(admin);
        Account account = accountService.getOne(new QueryWrapper<Account>().eq("type", "ADMIN").eq("person_id", adminId));
        account.setPassword(new BCryptPasswordEncoder().encode(newPassword));
        accountService.updateById(account);
    }

    @Override
    public void addAdmin(String username) {
//        Integer adminId = getAdminId();
//        if (adminId == null || adminId.equals("")) throw new WebException(YOU_DO_NOT_HAVE_THIS_POWER);

        Account account = accountService.getOne(new QueryWrapper<Account>().eq("username", username).eq("type", "ADMIN"));
        if (account != null) throw new WebException(USERNAME_IS_EXISTS);


        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(new BCryptPasswordEncoder().encode("111"));
        admin.setStatus("ENABLE");
        save(admin);

        Account account1 = new Account();
        account1.setUsername(username);
        account1.setPassword(new BCryptPasswordEncoder().encode("111"));
        account1.setType("ADMIN");
        Admin one = getOne(new QueryWrapper<Admin>().eq("username", username));
        account1.setPersonId(one.getId());
        accountService.save(account1);
    }
}
